'use strict';
/**
 * Created by deng on 16-1-19.
 */

angular.module('metalabPartnerApp').config(['$stateProvider', function ($stateProvider) {


    var detailModule = function (tag, per) {
        return ['MetaResources', '$stateParams', '$q', function (MetaResources, $stateParams, $q) {
            var defer = $q.defer();
            var page = parseInt($stateParams.page);
            MetaResources.achievement.query({
                talentId: $stateParams.talentId,
                'tag': tag,
                '_range': per ? (page - 1) * per + 1 + '_' + (page) * per : undefined
            }, function (data, headers) {
                console.log(data);
                var result = {};
                result.data = data;
                result.headers = headers();
                defer.resolve(result);
            }, function (error) {
                defer.reject(error);
            });
            return defer.promise;
        }];
    };

    /**
     * talent 路由
     */
    $stateProvider.state('meta.talent', {
        abstract: true,
        url: '/talent'
    }).state('meta.talent.list', {
        url: '/list',
        abstract: true,
        views: {
            'body@meta': {
                templateUrl: 'views/talent/list.html',
                controller: 'TalentListCtrl'
            }
        }
    }).state('meta.talent.list.all', {
        url: '?page',
        params: {
            page: '1'
        },
        views: {
            'body@meta': {
                templateUrl: 'views/talent/list.html',
                controller: 'TalentListCtrl'
            }
        }
    }).state('meta.talent.list.favorite', {
        url: '/favorite?page',
        params: {
            page: '1'
        },
        views: {
            'body@meta': {
                templateUrl: 'views/talent/list.html',
                controller: 'TalentListCtrl'
            }
        }
    }).state('meta.talent.search', {
        url: '/search?keywords&keywordList',
        views: {
            'body@meta': {
                templateUrl: 'views/talent/search.html',
                controller: 'TalentSearchCtrl'
            }
        }
    }).state('meta.talent.detail', {
        abstract: true,
        url: '/:talentId?page',
        params: {
            page: '1'
        },
        resolve: {
            talent: ['MetaResources', '$stateParams', function (MetaResources, $stateParams) {
                return MetaResources.talent.get(
                    {'id': $stateParams.talentId, '_embed': 'user'}
                ).$promise;
            }],
            project: detailModule('项目', 5),
            paper: detailModule('论文', 5),
            patent: detailModule('专利', 5)
        },
        views: {
            'body@meta': {
                templateUrl: 'views/talent/detail.html',
                controller: 'TalentDetailCtrl'
            }
        }
    }).state('meta.talent.detail.patent', {
        url: '/patent',
        views: {
            'tabData@meta.talent.detail': {
                templateUrl: 'views/talent/template/person-works.html',
                controller: 'AchievementsCtrl'
            }
        },
        data: {
            tag: '专利'
        }
    }).state('meta.talent.detail.project', {
        url: '/project',
        views: {
            'tabData': {
                templateUrl: 'views/talent/template/person-works.html',
                controller: 'AchievementsCtrl'
            }
        },
        data: {
            tag: '项目'
        }
    }).state('meta.talent.detail.paper', {
        url: '/paper',
        views: {
            'tabData': {
                templateUrl: 'views/talent/template/person-works.html',
                controller: 'AchievementsCtrl'
            }
        },
        data: {
            tag: '论文'
        }
    }).state('meta.talent.edit', {
        url: '/edit',
        resolve: {
            talent: function ($rootScope, MetaResources, $q) {
                var defer = $q.defer();
                $rootScope.passport.promise.then(function () {
                    MetaResources.talent.getByUser({userId: '_self', '_embed': 'user'}, function (data) {
                        defer.resolve(data);
                    }, function (error) {
                        MetaRemind.error(error);
                    });
                });
                return defer.promise;
            }
        },
        views: {
            'body@meta': {
                templateUrl: 'views/talent/edit.html',
                controller: 'TalentEditCtrl'
            }
        }
    })
    ;
}]);
